﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing.Printing;

namespace Lab1
{
    /// <summary>
    /// Interaction logic for Result.xaml
    /// </summary>
    public partial class Result : Page
    {
        string src = AppDomain.CurrentDomain.BaseDirectory + "report.htm";
        public Result()
        {
            InitializeComponent();

            protocolListBox.DataContext = UserAnswerService.UserAnswers.Values;
            resultList.DataContext = getReccomendation();
        }

        private void Print(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                printDialog.PrintVisual(resultGrid, "Отчет");
            }
            //using (StreamWriter sw = new StreamWriter(File.OpenWrite(src),Encoding.Unicode))
            //{
            //    sw.WriteLine("<html><head><title></title></head><body>");
            //    sw.WriteLine("<h2>Рекомендации</h2>");
            //    sw.WriteLine("<ul>");
            //    foreach (Recommendation item in getReccomendation())
            //    {
            //        sw.WriteLine("<li><h3>" + item.Title + "</h3><p>"
            //            + item.Description + "</p></li>");
            //    }
            //    sw.WriteLine("</ul>");
            //    sw.WriteLine("<h2>Протокол консультации</h2>");
            //    sw.WriteLine("<ul>");
            //    foreach (UserAnswer item in UserAnswerService.UserAnswers.Values)
            //    {
            //        sw.WriteLine("<li><h3>" + "№" + item.Number + "</h3><p><strong>Вопрос:</strong>"
            //            + item.Question.Title + "<br><strong>Ответ:</strong>");
            //        if (item.FreeAnswer != null)
            //        {
            //            sw.WriteLine(item.FreeAnswer + "</p></li>");

            //        }
            //        else
            //        {
            //            sw.WriteLine(item.AnswerVariant.Title + "</p></li>");
            //        }
            //    }
            //    sw.WriteLine("</ul>");
            //    sw.WriteLine("</body></html>");
       
            //    MessageBox.Show("Печать завершена!");
            //}
        }
        private IEnumerable<Recommendation> getReccomendation()
        {
            List<Recommendation> result = (new RecommendationService()).getAll().ToList();
            IEnumerable<Criterion> crit = (new CriterionService()).getAll();
            foreach (UserAnswer userAnswer in UserAnswerService.UserAnswers.Values)
            {
                try
                {
                    IEnumerable<Criterion> criterionList = (from t in crit
                                           where t.QuestionId.Equals(userAnswer.QuestionId)
                                           select t).ToList();
                    foreach (Criterion criterion in criterionList)
                    {
                        if (!userAnswer.AnswerVariantId.Equals(criterion.AnswerId))
                        {
                            //result.Remove((from t in result
                            //               where t.Id.Equals(criterion.RecommendationId)
                            //               select t).First());
                            
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
            return result;
        }
    }
}
